<?php

declare(strict_types=1);

namespace app\common\model\order;

use app\common\model\BaseModel;
use think\facade\Db;
use think\facade\Log;
use app\common\ConstVar;
use app\common\service\Util;
use app\common\service\third_party_api\ShipApiService;

class ShipOrder extends BaseModel
{
    // 订单状态常量
    const STATUS_NORMAL = 0;      // 正常
    const STATUS_CHANGED = 1;   // 改签
    const STATUS_PARTIAL_REFUND = 2;   // 部分退款
    const STATUS_FULL_REFUND = 3;   // 全部退款
    const STATUS_COMPLETED = 4;   // 已完成
    const DELAY_TIME = 270; //支付倒计时，过时自动取消订单

    protected $name = 'ship_order';

    protected $pk = 'id';
    protected $autoWriteTimestamp = 'int';
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';

    protected $type = [
        'create_time' => 'timestamp',
        'update_time' => 'timestamp',
        'departure_time' => 'timestamp',
        'arrival_time' => 'timestamp',
        'pay_time' => 'timestamp',
        'price' => 'float',
        'total_price' => 'float',
    ];
    
    /**
     * 字段验证规则
     * @var array
     */
    protected $schema = [
        'id'                => 'int',
        'order_sn'          => 'string',
        'self_order_no'     => 'string',
        'tc_order_no'       => 'string',
        'tc_voyage_no'      => 'string',
        'user_id'           => 'int',
        'booking_tel'       => 'string',
        'status'            => 'int',
        'price'             => 'float',
        'total_price'       => 'float',
        'passenger_info'    => 'string',
        'departure_time'    => 'timestamp',
        'arrival_time'      => 'timestamp',
        'ship_line_info'    => 'string',
        'shipline'          => 'string',
        'tc_refund_notify'  => 'string',
        'tc_notify'         => 'string',
        'create_time'       => 'timestamp',
        'update_time'       => 'timestamp',
        'need_sms'          => 'int',
        'pay_time'          => 'timestamp',
        'GetTicketNOResult' => 'int',
    ];

    public static function onBeforeInsert($model)
    {
        self::event('before_insert', function ($model) {
            if (empty($model->order_sn)) {
                $goodsOrderModel = new GoodsOrder();
                $model->order_sn = $goodsOrderModel->buildOrderSn();
            }
        });
    }

    /**
     * 关联乘船儿童信息
     * @return \think\model\relation\HasMany
     */
    public function takeChildren()
    {
        return $this->hasMany(ShipOrderTakeChildren::class, 'ship_order_id', 'id');
    }

    /**
     * 关联退款信息
     * @return \think\model\relation\HasOne
     */
    public function refundList()
    {
        return $this->hasOne(ShipOrderRefund::class, 'ship_order_id', 'id');
    }

    /**
     * 关联部分退款信息
     * @return \think\model\relation\HasMany
     */
    public function partialRefundList()
    {
        return $this->hasMany(ShipOrderPartialRefund::class, 'ship_order_id', 'id');
    }

    /**
     * 多态关联 - 关联到订单表
     * @return \think\model\relation\MorphOne
     */
    public function order()
    {
        return $this->morphOne(Order::class, 'orderable', 'ship');
    }
}
